var groupId;
var mode = 0; //0-管理  1-添加

var templateParams = {
    tableTheads:["节点名称", "IP", "用户名", "最大线程","当前执行线程", "状态", "工作目录", "创建时间", "创建人", "备注","操作"],
    btnTools:[{
        type:"primary",
        size:"M",
        id:"manger-slave",
        iconFont:"&#xe60c;",
        name:"管理执行机"
    },{
        size:"M",
        id:"add-slave",
        iconFont:"&#xe600;",
        name:"添加执行机"
    },{
        size:"M",
        id:"batch-op",
        iMarkClass:"Hui-iconfont-del3",
        name:"批量操作"
    }],
    formControls:[{
        edit:true,
        label:"ID",
        objText:"idText",
        input:[{
            hidden:true,
            name:"id"
        }]
    },
        {
            required:true,
            label:"节点名称",
            input:[{
                name:"slave_name"
            }]
        },
        {
            label:"IP",
            objText:"ipText",
            input:[{
                name:"ip",
                hidden: true
            }]
        },
        {
            label:"用户名",
            objText:"user_nameText",
            input:[{
                name:"username",
                hidden: true
            }]
        },
        {
            required:true,
            label:"最大执行线程",
            input:[{
                name:"parallel_max"
            }]
        },
        {
            required:true,
            label:"当前执行执行线程",
            input:[{
                name:"parallel_now"
            }]
        },
        {
            label:"状态",
            objText: "statusText",
            input:[{
                name:"status",
                hidden: true
            }]
        },
        {
            required: true,
            label:"工作目录",
            input:[{
                name:"work_path"
            }]
        },
        {
            label:"备注",
            textarea:[{
                name:"mark"
            }]
        }
    ]
};

var columnsSetting = [{
    "data":null,
    "render":function(data, type, full, meta){
        return checkboxHmtl(data.slave_name, data.id, "selectSlave");
    }},
    ellipsisData("slave_name"),
    ellipsisData("ip"),
    ellipsisData("username"),
    {"data": "parallel_max"},
    {"data": "parallel_now"},
    {"data": "status"},
    ellipsisData("work_path"),
    ellipsisData("create_time"),
    ellipsisData("creator_name"),
    longTextData("mark", "slave_name", "备注信息"),
    {
        "data":null,
        "render":function(data, type, full, meta){
            var context;
            //管理
            if (mode == 0) {
                context = [{
                    title:"编辑",
                    markClass:"object-edit",
                    iconFont:"&#xe6df;"
                }, {
                    title:"删除",
                    markClass:"del-group-slave",
                    iconFont:"&#xe6e2;"
                }, {
                    title:"断开连接",
                    markClass:"disconnet",
                    iconFont:"&#xe726;"
                }];
            }

            //添加
            if (mode == 1) {
                context = [{
                    title:"添加",
                    markClass:"add-group-slave",
                    iconFont:"&#xe600;"
                }];
            }
            return btnIconTemplate(context);
        }
    }
];


var eventList = {
    ".object-edit":function(){
        var data = table.row( $(this).parents('tr') ).data();
        publish.renderParams.editPage.modeFlag = 1;
        publish.renderParams.editPage.objId = data.id;
        publish.renderParams.editPage.currentObj = data;
        layer_show("编辑执行机信息", editHtml, editPageWidth, editPageHeight.edit, 1);
        publish.init();
    },
    "#batch-op":function() { //批量操作 删除或者添加
        let checkboxList = $(".selectSlave:checked");
        if (checkboxList.length < 1) {
            return false;
        }

        let opName = "删除";
        let url = REQUEST_URL.WEB_EXECUTION_MACHINE_GROUP.delMachineFromGroup + '&group_id=' + groupId;
        let sendData = [];
        if (mode == 1) {
            opName = "添加";
            url = REQUEST_URL.WEB_EXECUTION_MACHINE_GROUP.addMachine2Group + '&group_id=' + groupId;
        }

        $.each(checkboxList, function(i, n){
            let data = table.row( $(n).parents('tr') ).data();
            sendData.push(data.id);
        });

        layer.confirm('确定' + opName + '选中的' + checkboxList.length + '项吗？', {icon: 0, title: '提示'}, function(index){
            layer.close(index);
            GLOBAL_UTILS.ajaxUtils.ajaxPost(url, sendData, function(data){
                refreshTable();
                layer.msg(opName + '成功！', {icon: 1, time: 1600});
            });
        });
    },
    "#manger-slave":function() { //管理执行机
        var that = this;
        mode = 0;
        refreshTable(REQUEST_URL.WEB_EXECUTION_MACHINE.listMachine + "&group_id=" + groupId, function(json) {
            $(that).addClass('btn-primary').siblings("#add-slave").removeClass('btn-primary');
        }, null, true);
        $("#batch-op").children("i").removeClass("Hui-iconfont-add").addClass("Hui-iconfont-del3");
    },
    "#add-slave":function() { //添加执行机
        var that = this;
        mode = 1;
        refreshTable(REQUEST_URL.WEB_EXECUTION_MACHINE.listNotExistGroupMachine + "&group_id=" + groupId, function(json) {
            $(that).addClass('btn-primary').siblings("#manger-slave").removeClass('btn-primary');
        }, null, true);
        $("#batch-op").children("i").removeClass("Hui-iconfont-del3").addClass("Hui-iconfont-add");
    },
    //从组中删除
    ".del-group-slave":function(){
        var data = table.row( $(this).parents('tr') ).data();
        var that = this;
        layer.confirm('确定要从执行组中删除此执行机吗?', {icon:0, title:'警告'}, function(index) {
            layer.close(index);

            GLOBAL_UTILS.ajaxUtils.ajaxPost(REQUEST_URL.WEB_EXECUTION_MACHINE_GROUP.delMachineFromGroup + '&group_id=' + groupId, [data.id], function(data){
                table.row($(that).parents('tr')).remove().draw();
                layer.msg('删除成功!', {icon:1, time:1500});
            });
        });
    },
    //添加到执行组
    ".add-group-slave": function() {
        var data = table.row( $(this).parents('tr') ).data();
        layer.confirm('确定添加此执行机到执行组中吗?', {icon:0, title:'提示'}, function(index) {
            layer.close(index);

            GLOBAL_UTILS.ajaxUtils.ajaxPost(REQUEST_URL.WEB_EXECUTION_MACHINE_GROUP.addMachine2Group + '&group_id=' + groupId, [data.id] ,function(data){
                layer.msg('添加成功!', {icon:1, time:1500});
                refreshTable();
            });
        });
    },
    //断开连接
    ".disconnet": function(){
        let data = table.row( $(this).parents('tr') ).data();
        layer.confirm('确认断开该执行机的连接吗？', {title: '警告', icon: 0}, function(index){
            GLOBAL_UTILS.ajaxUtils.ajaxGet(REQUEST_URL.WEB_EXECUTION_MACHINE.breakSlave + "&slaveId=" + data.id, function(d){
                layer.msg('已断开!', {icon: 1, time: 1400});
                refreshTable();
            }, null, null, null, "正在断开连接...");
            layer.close(index);
        });
    }
};


var mySetting = {
    eventList:eventList,
    templateCallBack:function(df){
        groupId = GetQueryString("group_id");
        publish.renderParams.listPage.listUrl = REQUEST_URL.WEB_EXECUTION_MACHINE.listMachine + "&group_id=" + groupId;

        df.resolve();
    },
    listPage:{
        tableObj:".table-sort",
        columnsSetting:columnsSetting,
        columnsJson:[0, 11],
        dtOtherSetting:{
            serverSide:false
        },
        exportExcel:false
    },
    editPage:{
        editUrl:REQUEST_URL.WEB_EXECUTION_MACHINE.editMachine,
        getUrl:REQUEST_URL.WEB_EXECUTION_MACHINE.getMachine,
        rules:{
            slave_name:{
                required:true,
                minlength:2,
                maxlength:255
            },
            parallel_max: {
                required: true,
                digits: true
            },
            parallel_now: {
                required: true,
                digits: true
            },
            work_path: {
                required: true
            }
        }
    },
    templateParams:templateParams
};


$(function(){
    publish.renderParams = $.extend(true,publish.renderParams,mySetting);
    publish.init();
});